import redis
from libs.read_config import read_ini_value


class Redis:
    def __init__(self):
        self.host = read_ini_value('redis', 'host')
        self.port = read_ini_value('redis', 'port')
        self.db = read_ini_value('redis', 'db')
        self.username = read_ini_value('redis', 'username')
        self.password = read_ini_value('redis', 'password')

    def _get_connect(self):
        self.pool = redis.ConnectionPool(host=self.host, port=self.port, password=self.password, max_connections=10)
        self.conn = redis.Redis(connection_pool=self.pool)
        if self.conn.ping():
            print('redis连接成功！！！')
            return self.conn
        else:
            raise 'redis连接失败，请检查！！！'

    def flushall(self):
        try:
            self._get_connect().flushall()
            print('刷新缓存成功')
        except Exception:
            '刷新缓存失败'


if __name__ == '__main__':
    r = Redis()
    r.flushall()
